package com.cskaoyan.gateway.controller.shopping;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.shopping.IContentService;
import com.mall.shopping.IHomeService;
import com.mall.shopping.IProductCateService;
import com.mall.shopping.IProductService;
import com.mall.shopping.constants.ShoppingRetCode;
import com.mall.shopping.dto.*;
import com.mall.user.annotation.Anoymous;
import com.mall.user.intercepter.TokenIntercepter;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;


/**
 * @Time : 2021/8/25 16:18
 * @Author : Xfeng
 */

@Slf4j
@RestController
@RequestMapping("shopping")
@Api(tags = "GoodsController", description = "商品显示层")
public class GoodsController {

    @Reference(timeout = 3000,check = false)
    private IHomeService iHomeService;

    @Reference(timeout = 3000,check = false)
    private IContentService iContentService;

    @Reference(timeout = 3000,check = false)
    private IProductCateService iProductCateService;

    @Reference(timeout = 3000,check = false)
    private IProductService iProductService;


    @GetMapping("homepage")
    @ApiOperation("主页显示")
    @Anoymous
    public ResponseData homepage() {
        HomePageResponse response = iHomeService.homepage();

        if (response.getCode().equals(ShoppingRetCode.SUCCESS.getCode())) {
            return new ResponseUtil().setData(response.getPanelContentItemDtos());
        }
        return new ResponseUtil().setErrorMsg(response.getMsg());

    }

    @GetMapping("navigation")
    @ApiOperation("导航栏显示")
    @Anoymous
    public ResponseData navigation() {
        NavListResponse response = iContentService.queryNavList();

        if (response.getCode().equals(ShoppingRetCode.SUCCESS.getCode())) {
            return new ResponseUtil().setData(response.getPannelContentDtos());
        }
        return new ResponseUtil().setErrorMsg(response.getMsg());

    }

    @GetMapping("categories")
    @ApiOperation("列举所有商品种类")
    @Anoymous
    public ResponseData categories(AllProductCateRequest request) {
        AllProductCateResponse response = iProductCateService.getAllProductCate(request);

        if (response.getCode().equals(ShoppingRetCode.SUCCESS.getCode())) {
            return new ResponseUtil().setData(response.getProductCateDtoList());
        }
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }

    @GetMapping("product/{id}")
    @ApiOperation("查看商品详情")
    @Anoymous
    public ResponseData product(@PathVariable("id")Long id) {
        ProductDetailRequest request = new ProductDetailRequest();
        request.setId(id);
        ProductDetailResponse response = iProductService.getProductDetail(request);

        if (response.getCode().equals(ShoppingRetCode.SUCCESS.getCode())) {
            return new ResponseUtil().setData(response.getProductDetailDto());
        }
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }

    @GetMapping("goods")
    @ApiOperation("分页查询商品列表")
    @Anoymous
    public ResponseData goods(AllProductRequest request) {

        AllProductResponse response = iProductService.getAllProduct(request);

        if (response.getCode().equals(ShoppingRetCode.SUCCESS.getCode())) {
            response.setCode(null);
            response.setMsg(null);
            return new ResponseUtil().setData(response);
        }
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }

    @GetMapping("recommend")
    @ApiOperation("查询推荐商品")
    @Anoymous
    public ResponseData recommend() {

        RecommendResponse response = iProductService.getRecommendGoods();

        if (response.getCode().equals(ShoppingRetCode.SUCCESS.getCode())) {
            return new ResponseUtil().setData(response.getPanelContentItemDtos());
        }
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }

    @GetMapping("seckilllist")
    @ApiOperation("秒杀活动")
    public ResponseData seckilllist(Integer sessionId, HttpServletRequest servletRequest) {
        //从TokenInterceptor设置的request域中取出当前登录的userId
        String userInfo = (String) servletRequest.getAttribute(TokenIntercepter.USER_INFO_KEY);
        //Json字符串转换为Json对象
        JSONObject object = JSON.parseObject(userInfo);
        long userId = Long.parseLong(object.get("uid").toString());

        return new ResponseUtil().setErrorMsg("秒杀活动不存在");
    }

    @PostMapping("search")
    @ApiOperation("查询商品")
    @Anoymous
    public ResponseData search(@RequestBody AllProductRequest request) {

        AllProductResponse response = iProductService.getAllProduct(request);

        if (response.getCode().equals(ShoppingRetCode.SUCCESS.getCode())) {
            response.setCode(null);
            response.setMsg(null);
            return new ResponseUtil().setData(response);
        }
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }


}
